package com.hero.jobweb.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hero.basicsystem.dao.ICodeDAO;
import com.hero.basicsystem.dao.ext.ICodesortDAOExt;
import com.hero.basicsystem.entity.Code;
import com.hero.basicsystem.entity.CodeExample;
import com.hero.basicsystem.entity.Codesort;
import com.hero.basicsystem.entity.CodesortExample;

/**
 * 系统缓存数据库数据
 * 
 * @author Volcano
 */
public final class CacheData {
	private static final Log log = LogFactory.getLog(CacheData.class);
	private static final Map<Integer, Codesort> codeSortMap = new HashMap<Integer, Codesort>();// 代码分类
	private static final Map<Integer, Code> codeMap = new HashMap<Integer, Code>();// 代码
	private static final Map<String, Code> tradeMap = new HashMap<String, Code>();// 行业代码
	private static final Map<String, Code> positionMap = new HashMap<String, Code>();// 职位代码
	private static final Map<String, Code> areaMap = new HashMap<String, Code>();// 地域代码
	private static final Map<String, Code> degreeMap = new HashMap<String, Code>();// 学历代码
	private static final Map<String, Code> marriageMap = new HashMap<String, Code>();// 婚姻状态
	private static final Map<String, Code> salaryMonthMap = new HashMap<String, Code>();// 月薪
	private static final Map<String, Code> salaryYearMap = new HashMap<String, Code>();// 年薪
	private static final Map<String, Code> jobTypeMap = new HashMap<String, Code>();// 工作性质
	private static final Map<String, Code> startWorkTimeMap = new HashMap<String, Code>();// 上岗时间
	private static final Map<String, Code> enterpriseTypeMap = new HashMap<String, Code>();// 公司性质
	private static final Map<String, Code> majorMap = new HashMap<String, Code>();// 专业
	private static final Map<String, Code> languageMap = new HashMap<String, Code>();// 语言
	private static final Map<String, Code> languageLevelMap = new HashMap<String, Code>();// 语言水平
	private static final Map<String, Code> itSkillMap = new HashMap<String, Code>();// IT技能
	private static final Map<String, Code> itSkillLevelMap = new HashMap<String, Code>();// IT技能水平
	private static final Map<String, Code> workExperienceMap = new HashMap<String, Code>();// 工作经验
	private static final Map<String, Code> idTypeMap = new HashMap<String, Code>();// 证件类型
	private static final Map<String, Code> resumePubStateMap = new HashMap<String, Code>();//简历公开程度

	public static Map<String, Code> getResumePubStateMap() {
		return resumePubStateMap;
	}

	public static Map<String, Code> getIdTypeMap() {
		return idTypeMap;
	}

	/**
	 * @return the workexperiencemap
	 */
	public static Map<String, Code> getWorkExperienceMap() {
		return workExperienceMap;
	}

	/**
	 * @return the itskillmap
	 */
	public static Map<String, Code> getItSkillMap() {
		return itSkillMap;
	}

	/**
	 * @return the itskilllevelmap
	 */
	public static Map<String, Code> getItSkillLevelMap() {
		return itSkillLevelMap;
	}

	/**
	 * @return the getSalaryMonthMap
	 */
	public static Map<String, Code> getSalaryMonthMap() {
		return salaryMonthMap;
	}

	/**
	 * @return the getSalaryYearMap
	 */
	public static Map<String, Code> getSalaryYearMap() {
		return salaryYearMap;
	}

	/**
	 * @return the getJobTypeMap
	 */
	public static Map<String, Code> getJobTypeMap() {
		return jobTypeMap;
	}

	/**
	 * @return the getStartWorkTimeMap
	 */
	public static Map<String, Code> getStartWorkTimeMap() {
		return startWorkTimeMap;
	}

	/**
	 * @return the getEnterpriseTypeMap
	 */
	public static Map<String, Code> getEnterpriseTypeMap() {
		return enterpriseTypeMap;
	}

	/**
	 * @return the getMajorMap
	 */
	public static Map<String, Code> getMajorMap() {
		return majorMap;
	}

	/**
	 * @return the getLanguageMap
	 */
	public static Map<String, Code> getLanguageMap() {
		return languageMap;
	}

	/**
	 * @return the getLanguagelevelMap
	 */
	public static Map<String, Code> getLanguageLevelMap() {
		return languageLevelMap;
	}

	public static Map<String, Code> getTradeMap() {
		return tradeMap;
	}

	public static Map<String, Code> getDegreeMap() {
		return degreeMap;
	}

	public static Map<String, Code> getMarriageMap() {
		return marriageMap;
	}

	public static Map<String, Code> getAreaMap() {
		return areaMap;
	}

	public static Map<Integer, Codesort> getCodeSortMap() {
		return codeSortMap;
	}

	public static Map<Integer, Code> getCodeMap() {
		return codeMap;
	}

	public static Map<String, Code> getPositionMap() {
		return positionMap;
	}

	private CacheData() {
	}

	private static class NullCode extends Code {
		public NullCode() {
			setName("");
			setCode("");
			setUpCode("");
		}
	}

	/**
	 * 初始化数据
	 * 
	 * @param codeSortDAO
	 * @param codeDAO
	 */
	public static void initData(ICodesortDAOExt codeSortDAO, ICodeDAO codeDAO) {
		// initCodeSortMap(codeSortDAO);
		initCodeMap(codeDAO);
	}

	@SuppressWarnings("unused")
	private static void initCodeSortMap(ICodesortDAOExt codeSortDAO) {
		log.debug("初始化编码类别");
		List<Codesort> codeSortList = codeSortDAO
				.selectByExample(new CodesortExample());
		for (Codesort codesort : codeSortList) {
			codeSortMap.put(codesort.getId(), codesort);
		}
		log.debug("初始化编码类别完成");
	}

	private static void initCodeMap(ICodeDAO codeDAO) {
		log.debug("初始化编码");
		List<Code> codeList = codeDAO.selectByExample(new CodeExample());
		for (Code code : codeList) {
			// codeMap.put(code.getId(), code);
			if (codeSortIdCompare(code, SystemKey.TRADE_SORT_ID)) {
				addCode(tradeMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.POSITION_SORT_ID)) {
				addCode(positionMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.AREA_SORT_ID)) {
				addCode(areaMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.DEGREE_SORT_ID)) {
				addCode(degreeMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.MARRIAGE_SORT_ID)) {
				addCode(marriageMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.SALARY_MONTH_SORT_ID)) {
				addCode(salaryMonthMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.SALARY_YEAR_SORT_ID)) {
				addCode(salaryYearMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.JOB_TYPE_SORT_ID)) {
				addCode(jobTypeMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.START_WORK_TIME_SORT_ID)) {
				addCode(startWorkTimeMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.ENTERPRISE_TYPE_SORT_ID)) {
				addCode(enterpriseTypeMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.MAJOR_SORT_ID)) {
				addCode(majorMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.LANGUAGE_SORT_ID)) {
				addCode(languageMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.LANGUAGE_LEVEL_SORT_ID)) {
				addCode(languageLevelMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.IT_SKILL_SORT_ID)) {
				addCode(itSkillMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.IT_SKILL_LEVEL_SORT_ID)) {
				addCode(itSkillLevelMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.WORK_EXPERIENCE__SORT_ID)) {
				addCode(workExperienceMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.ID_TYPE_SORT_ID)) {
				addCode(idTypeMap, code);
				continue;
			}
			if (codeSortIdCompare(code, SystemKey.RESUME_PUB_STATE_SORT_ID)) {
				addCode(resumePubStateMap, code);
				continue;
			}

		}
		log.debug("初始化编码完成");
	}

	private static boolean codeSortIdCompare(Code code, SystemKey key) {
		return code.getCodeSortId().intValue() == MessagesManger
				.getSystemMessagesInt(key);
	}

	/**
	 * 增加代码
	 */
	public static void addCode(Map<String, Code> map, Code code) {
		map.put(code.getCode(), code);
	}
	/**
	 * 获取编码对应的Name
	 * @param map
	 * @param code
	 * @return
	 */
	public static String getName(Map<String, Code> map, String code) {
		Code codeValue = map.get(code);
		if (codeValue == null) {
			codeValue = new NullCode();
		}
		return codeValue.getName();
	}
	/**
	 * 获取以,分隔的多个Name
	 * @param map
	 * @param code
	 * @return
	 */
	public static String getMultiName(Map<String, Code> map, String code) {
		return getMultiName(map, code, false, null);
	}

	private static void appendMultiName(Map<String, Code> map,
			StringBuffer codeNameBuffer, String code, boolean isShowUp,
			String upSep) {
		if (isShowUp) {
			String upName = getUpName(map, code);
			if (!StringUtils.isEmpty(upName)) {
				codeNameBuffer.append(upName);
				codeNameBuffer.append(upSep);
			}
		}
		codeNameBuffer.append(getName(map, code));
	}

	public static String getMultiName(Map<String, Code> map, String code,
			boolean isShowUp, String upSep) {
		String[] codeArray = code.split(",");
		int codeArrayLength = codeArray.length;
		StringBuffer codeNameBuffer = new StringBuffer();
		for (int i = 0; i < codeArrayLength - 1; i++) {
			appendMultiName(map, codeNameBuffer, codeArray[i], isShowUp, upSep);
			codeNameBuffer.append(",");
		}
		if (codeArrayLength > 0) {
			appendMultiName(map, codeNameBuffer,
					codeArray[codeArrayLength - 1], isShowUp, upSep);
		}
		return codeNameBuffer.toString();
	}
	
	
	/**
	 * 获取上级Name
	 * @param map
	 * @param code
	 * @return
	 */
	public static String getUpName(Map<String, Code> map, String code) {
		Code currentCode = map.get(code);
		if (currentCode == null) {
			currentCode = new NullCode();
		}
		Code upCode = map.get(currentCode.getUpCode());
		if (upCode == null) {
			upCode = new NullCode();
		}
		return upCode.getName();
	}

}
